﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharePointees.EnhancedLookupField.Tools.Trace
{
    public static class TraceManager
    {
        private const string PROJECT_NAME_FORMAT = "SharePointees - {0}";


        /// <summary>
        /// Write information messages into SharePoint's logs
        /// </summary>
        public static void WriteMessage(string appName, string message)
        {
            TraceProvider.RegisterTraceProvider();
            TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.InformationEvent, Guid.Empty, string.Format(PROJECT_NAME_FORMAT, appName), appName, "Message", message);
            TraceProvider.UnregisterTraceProvider();
        }

        /// <summary>
        /// Write an exception into SharePoint's log
        /// </summary>
        public static void WriteError(string appName, Exception error)
        {
            TraceProvider.RegisterTraceProvider();

            TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.Exception, Guid.Empty, string.Format(PROJECT_NAME_FORMAT, appName), appName, "Error", string.Concat(error.Message , error.Source , error.StackTrace));

            if (error.InnerException != null)
                TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.Exception, Guid.Empty, string.Format(PROJECT_NAME_FORMAT, appName), appName, "Error", string.Concat(error.InnerException.Message, error.InnerException.Source, error.InnerException.StackTrace));

            TraceProvider.UnregisterTraceProvider();
        }
    }
}
